/**   
 * @Title: GeneralExceptionHandler.java 
 * CopyRight(c)2010-DMX 
 */
package com.dmx.fw.exception;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.sql.BatchUpdateException;
import java.sql.SQLException;
import java.sql.SQLWarning;

import org.apache.log4j.Logger;


/**
 * 通用异常处理
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003-2-19</p>
 * <p>Company: </p>
 * @author siyipin
 * @version 1.0
 */
public class GeneralExceptionHandler{
	private static Logger logger = Logger.getLogger(GeneralExceptionHandler.class);

    public static GeneralException Handle(Throwable eThrow){
    	logger.debug(eThrow);
        GeneralException eGenenal;
        if(eThrow instanceof GeneralException)
            eGenenal = (GeneralException)eThrow;
        else if(eThrow instanceof ArrayIndexOutOfBoundsException)
            eGenenal = new GeneralException(SystemMessage.ArrayIndexOutOfBoundsException,eThrow.getMessage());
        else if(eThrow instanceof ArrayStoreException)
            eGenenal = new GeneralException(SystemMessage.ArrayStoreException,eThrow.getMessage());
        else if(eThrow instanceof ClassCastException)
            eGenenal = new GeneralException(SystemMessage.ClassCastException,eThrow.getMessage());
        else if(eThrow instanceof NumberFormatException)
            eGenenal = new GeneralException(SystemMessage.NumberFormatException,eThrow.getMessage());
        else if(eThrow instanceof NullPointerException)
            eGenenal = new GeneralException(SystemMessage.NullPointerException,eThrow.getMessage());
        else if(eThrow instanceof FileNotFoundException)
            eGenenal = new GeneralException(SystemMessage.FileNotFoundException,eThrow.getMessage());
        else if(eThrow instanceof IOException)
            eGenenal = new GeneralException(SystemMessage.IOException,eThrow.getMessage());
        else if(eThrow instanceof IndexOutOfBoundsException)
            eGenenal = new GeneralException(SystemMessage.IndexOutOfBoundsException,eThrow.getMessage());
        else if(eThrow instanceof ClassNotFoundException)
            eGenenal = new GeneralException(SystemMessage.ClassNotFoundException,eThrow.getMessage());
        else if(eThrow instanceof NoSuchMethodException)
            eGenenal = new GeneralException(SystemMessage.NoSuchMethodException,eThrow.getMessage());
        else if(eThrow instanceof SecurityException)
            eGenenal = new GeneralException(SystemMessage.SecurityException,eThrow.getMessage());
        else if(eThrow instanceof SQLException){
            if(eThrow.getMessage().indexOf("unique constraint") >= 0)
                eGenenal = new GeneralException(SystemMessage.SaveunSingleException,eThrow.getMessage());
            else if(eThrow.getMessage().indexOf("null") >= 0)
                eGenenal = new GeneralException(SystemMessage.SaveWithNullException,eThrow.getMessage());
            else if(((SQLException)eThrow).getErrorCode()==1){
            	eGenenal = new GeneralException(SystemMessage.DoublePrimaryKeyException, eThrow.getMessage());
            }else{
                eGenenal = new GeneralException(SystemMessage.SQLException, eThrow.getMessage());
            }
        } else if(eThrow instanceof SQLWarning)
            eGenenal = new GeneralException(SystemMessage.SQLWarning,eThrow.getMessage(), "WARNING");
        else if(eThrow instanceof BatchUpdateException)
            eGenenal = new GeneralException(SystemMessage.BatchUpdateException,eThrow.getMessage());
        else if(eThrow instanceof RemoteException)
            eGenenal = new GeneralException(SystemMessage.RemoteException,eThrow.getMessage());
        else if(eThrow instanceof InvocationTargetException)
            eGenenal = new GeneralException(SystemMessage.InvocationTargetException,eThrow.getMessage());
        else if(eThrow.getMessage() != null && !eThrow.getMessage().trim().equals(""))
            eGenenal = new GeneralException(SystemMessage.UnknownException, eThrow.getMessage());
        else {
        	String exClassName = eThrow.getClass().getName();
        	// Check if EJB exception
	        if (exClassName.equals("javax.naming.NamingException"))
	            eGenenal = new GeneralException(SystemMessage.NamingException,eThrow.getMessage());
	        else if (exClassName.equals("javax.ejb.CreateException"))
	            eGenenal = new GeneralException(SystemMessage.CreateException,eThrow.getMessage());
	        else if(exClassName.equals("javax.ejb.EJBException"))
	            eGenenal = new GeneralException(SystemMessage.EJBException,eThrow.getMessage());
	        else if(exClassName.equals("javax.ejb.FinderException"))
	            eGenenal = new GeneralException(SystemMessage.FinderException,eThrow.getMessage());
	        else if(exClassName.equals("javax.ejb.NoSuchEntityException"))
	            eGenenal = new GeneralException(SystemMessage.NoSuchEntityException,eThrow.getMessage());
	        else if(exClassName.equals("javax.ejb.ObjectNotFoundException"))
	            eGenenal = new GeneralException(SystemMessage.ObjectNotFoundException,eThrow.getMessage());
	        else if(exClassName.equals("javax.ejb.RemoveException"))
	            eGenenal = new GeneralException(SystemMessage.RemoveException,eThrow.getMessage());
	        else
	        	eGenenal = new GeneralException(SystemMessage.UnknownException, eThrow.getMessage());
        }
        return eGenenal;
    }

}
